angular.module('wf-utils')

.service('commonUtil', ($state,$http,$q) ->
    test:->
        angular.noop()
    transferGetParam:(data)->
        if data instanceof Object and data != null and data != undefined
            str = '?'
            for param of data
                if data[param] != undefined and data[param] != ''
                    str += param + '=' + data[param] + '&'
            str = str.substring(0, str.length - 1)
            str
        else
            ''
    cloneObj:(obj) ->
        if obj instanceof Object and obj != null
            item = {}
            for param of obj
                item[param] = obj[param]
            item
        else
            {}
    cloneArrs:(arrs) ->
        if arrs instanceof Array
            list = []
            for i of arrs
                item = @cloneObj(arrs[i])
                list.push item
            list
        else
            []
    checkEmptyStr:(args) ->
        if args instanceof Array
            for i of args
                if args[i] == undefined or args[i] == ''
                    return false
        else
            if args == undefined or args == ''
                return false
        true
    batchRemove:(url,id,checkedItems)->
        ids=[]
        if id
            ids=[{id:id}]
        else
            _.each checkedItems,(value,key)->
                if value==true
                    ids.push {id:key}
        def=$q.defer()
        promise=def.promise
        count=0
        _.each ids,(item)->
            $http.delete(url+item.id)
            .success((resp)->
                count++
                item.done=if(resp.code==0) then true else false
                item.message=resp.message
                if(count==ids.length)
                    def.resolve(ids)
            )
            .error((err)->
                count++
                item.done=false
                item.message='删除数据异常'
                if(count==ids.length)
                    def.resolve()
            )
        promise
    pad:(str, n)->
        len = str.length;
        if len>n
            return str.substr(len-n,len)
        else
            while len < n
                str = "0" + str
                len++
            return str
    getBinListValue:(value,theList,key,val)->
        key=key || 'value'
        val=val || 'name'
        list=angular.copy(theList)
        list=_.filter(list,(item)->item[key]!=undefined)
        binVal=value.toString(2)
        binVal=this.pad(binVal,list.length)
        binList=binVal.split('').reverse()
        nameList=[]
        for val,i in binList
            if binList[i]=='1'
                nameList.push(list[i].name)
        return nameList.join('/')
    loadBinList:(value,theList,key,val)->
        key=key || 'value'
        val=val || 'name'
        list=angular.copy(theList)
        list=_.filter(list,(item)->item[key]!=undefined)
        if value==undefined  || value==0
            return list
        binVal=value.toString(2)
        binVal=this.pad(binVal,list.length)
        binList=binVal.split('').reverse()
        for i in binList
            if binList[i]=='1'
                list[i].checked=true
        return list
)